#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct Bookinfor
{
	char bookID[10];/*书号*/
	char bookname[30];/*书名*/
	int num;/*数量*/
};/*书籍*/

struct Reader
{
	char ID[10];/*序号*/
	char name[20];/*人名*/
	char sex;/*性别*/
	int age;/*年龄*/
	char number[15];/*电话号码*/
};/*读者*/

struct Admin
{
	char ID[10];/*管理员序号*/
	char name[20];/*管理员人名*/
	char sex;/*性别*/
	int age;/*年龄*/
	char number[15];/*电话号码*/
	double salary;/*工资*/
};/*管理员*/

struct Borrowinfor
{
	char ID[10];/*书号*/
	char name[20];/*读者名*/
	char book[30];/*书名*/
	char borrowtime[20];/*借书时间*/
	char retime[20];/*还书时间*/
	int day;/*借阅时长*/
	double money;/*付费金额*/
};/*借阅情况*/
     
void Welcome()  /*欢迎界面*/
{
	printf("*************************************************\n");
	printf("*\t\t大学图书馆信息管理系统\t\t*\n");
	printf("*************************************************\n");
	printf("*\t\t请选择功能列表\t\t\t*\n");
	printf("*************************************************\n");
	printf("*\t\t1.书籍信息的录入\t\t*\n");
	printf("*\t\t2.读者信息的录入\t\t*\n");
	printf("*\t\t3.管理员信息的录入\t\t*\n");
	printf("*\t\t4.借阅情况的录入\t\t*\n");
	printf("*\t\t5.查询书籍信息\t\t\t*\n");
	printf("*\t\t6.查询读者信息\t\t\t*\n");
	printf("*\t\t7.查询管理员信息\t\t*\n");
	printf("*\t\t8.查询借阅情况\t\t\t*\n");
	printf("*\t\t9.修改书籍信息\t\t\t*\n");
	printf("*\t\t10.修改读者信息\t\t\t*\n");
	printf("*\t\t11.修改管理员信息\t\t*\n");
	printf("*\t\t12.修改借阅情况\t\t\t*\n");
	printf("*\t\t13.删除读者信息\t\t\t*\n");
	printf("*\t\t14.删除管理员信息\t\t*\n");
	printf("*\t\t15.保存所有信息\t\t\t*\n");
	printf("*\t\t16.读取所有信息\t\t\t*\n");
	printf("*\t\t17.退出\t\t\t\t*\n");
	printf("*************************************************\n");
}

void Input_book(Bookinfor books[], int bookNum)  /*录入书籍信息*/
{
	int i;
	
	for(i=0; i<bookNum; i++)
	{
	printf("请输入书籍ID:\n");
	getchar();
	scanf("%s", books[i].bookID);
	printf("请输入书名:\n");
	getchar();
	scanf("%s", books[i].bookname);
	printf("请输入书籍数量:\n");
	getchar();
	scanf("%d", &books[i].num);
	printf("\n");
    }
    
	system("pause");
	system("cls");
}

void Input_reader(Reader rea[], int reaNum)
{
	int i;
	
	for(i=0; i<reaNum; i++)
	{
		printf("请输入ID:\n");
		getchar();
		scanf("%s", rea[i].ID);
		printf("请输入人名:\n");
		getchar();
		scanf("%s", rea[i].name);
		printf("请输入性别:\n");
		getchar();
		scanf("%c", &rea[i].sex);
		printf("请输入年龄:\n");
		scanf("%d", &rea[i].age);
		printf("请输入电话号码:\n");
		getchar();
		scanf("%s", rea[i].number);
		printf("\n");
	}
	
	system("pause");
	system("cls");
}

/*录入管理员信息*/
void Input_adm(Admin adm[], int admNum)
{
	int i;
	
	for(i=0; i<admNum; i++)
	{
		printf("请输入ID:\n");
		getchar();
		scanf("%s", adm[i].ID);
		printf("请输入人名:\n");
		getchar();
		scanf("%s", adm[i].name);
		printf("请输入性别:\n");
		getchar();
		scanf("%c", &adm[i].sex);
		printf("请输入年龄:\n");
		scanf("%d", &adm[i].age);
		printf("请输入电话号码:\n");
		getchar();
		scanf("%s", adm[i].number);
		printf("请输入薪资:\n");
		scanf("%lf", &adm[i].salary);
		printf("\n");
	}
	
	system("pause");
	system("cls");
}

/*录入借阅情况*/
void Input_borrowinfor(Borrowinfor bor[], int borrowNum)
{
	int i;
	
	for(i=0; i<borrowNum; i++)
	{
	    printf("请输入ID:\n");
		getchar();
		scanf("%s", bor[i].ID);
		printf("请输入人名:\n");
		getchar();
		scanf("%s", bor[i].name);
		printf("请输入书名:\n");
		getchar();
		scanf("%s", bor[i].book);
		printf("请输入借书日期:\n");
		getchar();
		scanf("%s", bor[i].borrowtime);
		printf("请输入还书日期:\n");
		getchar();
		scanf("%s", bor[i].retime);
		printf("请输入借阅时长:\n");
		scanf("%d", &bor[i].day);
		
		if (bor[i].day > 3)
		{
			bor[i].money = (bor[i].day-3)*10; 
		}
		else
		{
			bor[i].money = 0;
		}
		
		printf("\n");
	}
	
	system("pause");
	system("cls");
}

/*保存书籍信息*/
void Save_book(Bookinfor books[], int bookNum)
{
	int i;
	FILE *fp1 = fopen("D:/test/book","wb");  /*打开文件*/;
	
	if (fp1 == NULL)
	{
		printf("未找到书籍信息文件.\n");
		system("pause");
	    system("cls");
		return;
	}
	
	for(i = 0; i<bookNum; i++)
	{
		fwrite(&books[i], sizeof(struct Bookinfor), 1, fp1);
	}
	
	fclose(fp1);
	printf("书籍信息保存成功.\n");
}

/*保存读者信息*/
void Save_reader(Reader rea[], int reaNum)  
{
	int i;
	FILE *fp2 = fopen("D:/test/reader","wb");
	
	if (fp2 == NULL)
	{
		printf("未找到读者信息文件.\n");
		system("pause");
	    system("cls");
		return;
	}
	
	for(i=0; i<reaNum; i++)
	{
		fwrite(&rea[i], sizeof(struct Reader), 1, fp2);
	}
	
	fclose(fp2);
	printf("读者信息保存成功.\n");
}

/*保存管理员信息*/
void Save_admin(Admin adm[], int admNum)
{
	int i;
	FILE *fp3 = fopen("D:/test/adm", "wb");
	
	if (fp3 == NULL)
	{
		printf("未找到管理员信息文件.\n");
		system("pause");
	    system("cls");
		return;
	}
	
	for(i=0; i<admNum; i++)
	{
		fwrite(&adm[i], sizeof(struct Admin), 1, fp3);
	}
	
	fclose(fp3);
	printf("管理员信息保存成功.\n");
}

/*保存借阅信息*/
void Save_borrowinfor(Borrowinfor bor[], int borrowNum)
{
	int i;
	FILE *fp4 = fopen("D:/test/borrow","wb");
	
	if (fp4 == NULL)
	{
		printf("未找到借阅情况文件.\n");
		system("pause");
	    system("cls");
		return;
	}
	
	for(i=0; i<borrowNum; i++)
	{
		fwrite(&bor[i], sizeof(struct Borrowinfor), 1, fp4);
	}
	
	fclose(fp4);
	printf("借阅信息保存成功.\n");
}

/*读取书籍信息*/
int Read_book(Bookinfor books[])
{
	FILE *fp1 = fopen("D:/test/book","rb");  /*打开文件*/
	int i = 0;
	int bookNum = 0;
	
	if (fp1 == NULL)
	{
		printf("未找到书籍信息文件.\n");
		system("pause");
	    system("cls");
		return bookNum;
	}
	while (fread(&books[i], sizeof(struct Bookinfor), 1, fp1) == 1)
	{
		i++;
	}
	bookNum = i;
	
		fclose(fp1);  /*关闭文件*/		
		printf("书籍信息读取成功.\n");
		
		return bookNum;
}

/*读取读者信息*/
int Read_reader(Reader rea[])
{
	int i = 0;
	int reaNum = 0;
	FILE *fp2 = fopen("D:/test/reader", "rb");
	
	if (fp2 == NULL)
	{
		printf("未找到读者信息文件.\n");
		system("pause");
	    system("cls");
		return reaNum;
	}
	
	while (fread(&rea[i], sizeof(struct Reader), 1, fp2) == 1)
	{
		i++;
	}
	reaNum = i;
	
	fclose(fp2);
	printf("读者信息读取成功.\n");
	
	return reaNum;
}

/*读取管理员信息*/
int Read_admin(Admin adm[])
{
	int i = 0;
	int admNum = 0;
	FILE *fp3 = fopen("D:/test/adm","rb");
	
	if (fp3 == NULL)
	{
		printf("未找到管理员信息文件.\n");
		system("pause");
	    system("cls");
		return admNum;
	}
	
	while (fread(&adm[i], sizeof(struct Admin), 1, fp3))
	{
		i++;
	}
	admNum = i;
	
	fclose(fp3);
	printf("管理员信息读取成功.\n");
	
	return admNum;
}

/*读取借阅信息*/
int Read_borrowinfor(Borrowinfor bor[])
{
	int i = 0;
	int borrowNum = 0;
	FILE *fp4 = fopen("D:/test/borrow", "rb");
	
	if (fp4 == NULL)
	{
		printf("未找到借阅情况文件.\n");
		system("pause");
	    system("cls");
	    return borrowNum;
	}
	
	while (fread(&bor[i], sizeof(struct Borrowinfor), 1, fp4))
	{
		i++;
	}
	borrowNum = i;
	
	fclose(fp4);
	printf("借阅信息读取成功.\n");
	
	return borrowNum;
}

/*查询书籍信息*/
void Check_book(Bookinfor books[], int bookNum)
{
	int i;
	
	printf("*************************************************\n");
	printf("*\t书号\t*\t书名\t*\t库存\t*\n");
	printf("*************************************************\n");
	
	for(i=0; i<bookNum; i++)
	{
		printf("*\t%s\t*\t%s\t*\t%d\t*\n", books[i].bookID, books[i].bookname, books[i].num);
	}
	
	system("pause");
	system("cls");
}

/*查询读者信息*/
void Check_reader(Reader rea[], int reaNum)
{
	int i;
	int flag = 0;  /*标记是否找到该用户(0没找到，1找到)*/
	char code[10];
	printf("请输入您的ID:\n");
	scanf("%s", code);
	system("cls");
	
	printf("*****************************************************************************************\n");
	printf("*\tID\t*\t名字\t*\t性别\t*\t年龄\t*\t电话\t\t*\n");
	printf("*****************************************************************************************\n");
	
	for(i=0; i<reaNum; i++)
	{
		if (strcmp(rea[i].ID, code) == 0)
		{
			flag = 1;
			printf("*\t%s\t*\t%s\t*\t%c\t*\t%d\t*\t%s\t*\n", rea[i].ID, rea[i].name, rea[i].sex, rea[i].age, rea[i].number);
		}
	}
	
	if (flag == 0)
	{
		printf("ID错误，该用户不存在.\n");
	}
	
	system("pause");
	system("cls");
}

/*查询管理员信息*/
void Check_admin(Admin adm[], int admNum)
{
	int i;
	int flag = 0;  /*标记是否找到该用户(0没找到，1找到)*/
	char code[10];
	
	printf("请输入您的ID:\n");
	scanf("%s", code);
	system("cls");
	
	printf("*****************************************************************************************\n");
	printf("*\tID\t*\t名字\t*\t性别\t*\t年龄\t*\t电话\t\t*\t薪资\t*\n");
	printf("*****************************************************************************************\n");
	
	for(i=0; i<admNum; i++)
	{
		if (strcmp(adm[i].ID, code) == 0)
		{
			flag = 1;
			printf("*\t%s\t*\t%s\t*\t%c\t*\t%d\t*\t%s\t*\t%lf\t*\n", adm[i].ID, adm[i].name, adm[i].sex, adm[i].age, adm[i].number, adm[i].salary);
		}
	}
	
	if (flag == 0)
	{
		printf("ID错误，该管理员不存在.\n");
	}
	
	system("pause");
	system("cls");
}

/*查询借阅情况*/
void Check_borrowinfor(Borrowinfor bor[], int borrowNum)
{
	int i;
	
	printf("*************************************************************************************************************************************************\n");
	printf("*\tID\t*\t名字\t*\t借阅书名\t*\t借阅日期\t*\t还书日期\t*\t借阅时长\t*\t支付金额\t*\n");
	printf("*************************************************************************************************************************************************\n");
	
	for(i=0; i<borrowNum; i++)
	{
		printf("*\t%s\t*\t%s\t*\t%s\t*\t%s\t*\t%s\t*\t%d\t*\t%lf\t*\n", bor[i].ID, bor[i].name, bor[i].book, bor[i].borrowtime, bor[i].retime, bor[i].day, bor[i].money);
	}
	
	system("pause");
	system("cls");
}

/*修改书籍信息*/
void Revise_book(Bookinfor books[], int bookNum)
{
	int i;
	int Newnum;
	int flag = 0;/*标记是否找到*/
	char code[10];
	char Newname[30];	
	
	printf("请输入要修改的书号:\n");
	scanf("%s", code);
	
	for(i=0; i<bookNum; i++)
	{
		if (strcmp(books[i].bookID, code) == 0)
		{
			flag = 1;
			printf("请输入修改后的名字:\n");
			getchar();
			scanf("%s", Newname);
			printf("请输入修改后的数量:\n");
			getchar();
			scanf("%d", &Newnum);
			
			strcpy(books[i].bookname, Newname);
			books[i].num = Newnum;
			
			printf("信息修改成功.\n");
		}
	}
	
	if (flag == 0)
	{
		printf("ID输入错误，该书籍序号不存在.\n");
	}
	
	system("pause");
	system("cls");
}

/*修改读者信息*/
void Revise_reader(Reader rea[], int reaNum)
{
	int i;
	int Newage;
	int flag = 0;
	char code[10];
	char Newname[20];
	char Newsex;
	char Newnumber[15];
	
	printf("请输入要修改的读者号:\n");
	scanf("%s", code);
	
	for(i=0; i<reaNum; i++)
	{
		if(strcmp(rea[i].ID, code) == 0)
		{
			flag = 1;
			printf("请输入修改后的名字:\n");
			getchar();
			scanf("%s", Newname);
			printf("请输入修改后的性别:\n");
			getchar();
			scanf("%c", &Newsex);
			printf("请输入修改后的年龄:\n");
			scanf("%d", &Newage);
			printf("请输入修改后的电话号码:\n");
			getchar();
			scanf("%s", &Newnumber);
			
			strcpy(rea[i].name, Newname);
			rea[i].sex = Newsex;
			rea[i].age = Newage;
			strcpy(rea[i].number, Newnumber);
			
			printf("信息修改成功.\n");
		}
	}
	
	if (flag == 0)
	{
		printf("ID输入错误，该读者不存在.\n");
	}
	
	system("pause");
	system("cls");
}

/*修改管理员信息*/
void Revise_admin(Admin adm[], int admNum)
{
	int i;
	int flag = 0;
	int Newage;
	double Newsalary;
	char code[10];
	char Newsex;
	char Newname[20];
	char Newnumber[15];
	
	printf("请输入要修改的ID:\n");
	scanf("%s", code);
	
	for(i=0; i<admNum; i++)
	{
		if (strcmp(adm[i].ID, code) == 0)
		{
			flag = 1;
			printf("请输入修改后的名字:\n");
			getchar();
			scanf("%s", Newname);
			printf("请输入修改后的性别:\n");
			getchar();
			scanf("%c", &Newsex);
			printf("请输入修改后的年龄:\n");
			scanf("%d", &Newage);
			printf("请输入修改后的电话号码:\n");
			getchar();
			scanf("%s", &Newnumber);
			printf("请输入修改后的薪资:\n");
			scanf("%lf", &Newsalary);
			
			strcpy(adm[i].name, Newname);
			adm[i].sex = Newsex;
			adm[i].age = Newage;
			strcpy(adm[i].number, Newnumber);
			adm[i].salary = Newsalary;
			
			printf("信息修改成功.\n");
		}
	}
	
	if (flag == 0)
	{
		printf("ID输入错误，该管理员不存在.\n");
	}
	
	system("pause");
	system("cls");
}

/*修改借阅情况*/
void Revise_borrowinfor(Borrowinfor bor[], int borrowNum)
{
	int i;
	int flag = 0;
	int Newday;
	double Newmoney;
	char code[10];
	char Newname[20];
	char Newbook[30];
	char Newborrowtime[20];
	char Newretime[20];
	
	printf("请输入要修改的ID:\n");
	scanf("%s", code);
	for(i=0; i<borrowNum; i++)
	{
		if (strcmp(bor[i].ID, code) == 0)
		{
			flag = 1;
			printf("请输入修改后的读者名:\n");
			getchar();
			scanf("%s", Newname);
			printf("请输入修改后的书名:\n");
			getchar();
			scanf("%s", Newbook);
			printf("请输入修改后的借书时间:\n");
			getchar();
			scanf("%s", Newborrowtime);
			printf("请输入修改后的还书时间:\n");
			getchar();
			scanf("%s", Newretime);
			printf("请输入修改后的借阅时长:\n");
			scanf("%d", &Newday);
			
			strcpy(bor[i].name, Newname);
			strcpy(bor[i].book, Newbook);
			strcpy(bor[i].borrowtime, Newborrowtime);
			strcpy(bor[i].retime, Newretime);
			bor[i].day = Newday;
			
			if (Newday > 3)
			{
				bor[i].money = (Newday-3)*10;
			}
			else
			{
				bor[i].money = 0;
			}
			
			printf("信息修改成功.\n");
		}
	}
	
	system("pause");
	system("cls");
}

/*删除读者信息*/
int Delete_reader(Reader rea[], int reaNum)
{
	int i;
	int j = 0;
	int flag;
	char code[10];
	
	printf("请输入要删除的读者ID:\n");
	scanf("%s", code);
	
	for(i=0; i<reaNum; i++)
	{
		if (strcmp(rea[i].ID, code) == 0)
		{
			flag = 1;
			j = i;
			reaNum--;
			
			for(i=j; i<reaNum; i++)
			{
				rea[i] = rea[i+1];
			}
			
			printf("读者信息删除成功.\n");
		}
	}
	if (flag == 0)
	{
		printf("ID输入错误，该读者不存在.\n");
	}
	
	system("pause");
	system("cls");
	return reaNum;
}

/*删除管理员信息*/
int Delete_admin(Admin adm[], int admNum)
{
	int i;
	int j = 0;
	int flag = 0;
	char code[10];
	
	printf("请输入要删除的管理员ID:\n");
	scanf("%s", code);
	
	for(i=0; i<admNum; i++)
	{
		if (strcmp(adm[i].ID, code) == 0)
		{
			flag = 1;
			j = i;
			admNum--;
			
			for(i=j; i<admNum; i++)
			{
				adm[i] = adm[i+1];
			}
			
			printf("管理员信息删除成功.\n");
		}
	}
	
	if (flag == 0)
	{
		printf("ID输入错误，该管理员不存在.\n");
	}
	
	system("pause");
	system("cls");
	return admNum;
}